/**
 * 
 */
package cn.micai.core.web;

import cn.micai.common.web.session.SessionProvider;
import cn.micai.core.bean.user.Admin;
import cn.micai.core.bean.user.Buyer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台登录控制拦截器
 * 处理上下文
 * 处理Session
 * 全局
 * 
 * @author 赵新国
 *
 */
public class BackLoginInterceptor implements HandlerInterceptor{
	
	@Autowired
	private SessionProvider sessionProvider;

	//常量
	private static final String INTERCEPTOR_URL = "/control/";
	
	//方法前 /buyer/
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Admin admin = (Admin) sessionProvider.getAttribute(request, Constants.ADMIN_SESSION);
		boolean flag = false;
		if(null != admin){
			flag = true;
			request.setAttribute("admin", admin);
		}
		request.setAttribute("isLogin", flag);

		//是否拦截 http://localhost:8080/control/login.do
		//	/control/login.do
		String requestURI = request.getRequestURI();
		if(requestURI.startsWith(INTERCEPTOR_URL)){
			//必须登录
			if(null == admin){
				response.sendRedirect("/control/login.do");
				return false;
			}
		}
		return true;
	}

	//方法后
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	//页面渲染后
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
